#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int t,n,a[2005];
ll dp[2005][2005];
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    scanf("%d",&t);
    while(t--){
        memset(dp,-0x3f,sizeof(dp));
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        dp[0][0]=0;
        ll ans=0;
        for(int i=1;i<=n;i++){
            ll res=0;
            for(int j=i-1;j;res+=(a[j+1]==a[j])*a[j],j--)for(int k=0;k<j;k++)dp[i][j]=max(dp[i][j],dp[j][k]+res+(a[j+1]==a[k])*a[k]);
            dp[i][0]=res;
        }
        for(int j=0;j<n;j++)ans=max(ans,dp[n][j]);
        printf("%lld\n",ans);
    }
}
